package cn.com.woxitech.dev.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.fasterxml.jackson.databind.JsonNode;

import cn.com.woxitech.dev.dao.gen.TPptCodeMapper;
import cn.com.woxitech.dev.dao.gen.WmCatalogRoomMapper;
import cn.com.woxitech.dev.dao.gen.WmRoomChairMapper;
import cn.com.woxitech.dev.dao.gen.WmRoomChathistoryMapper;
import cn.com.woxitech.dev.dao.gen.WmRoomLikeMapper;
import cn.com.woxitech.dev.dao.gen.WmRoomMapper;
import cn.com.woxitech.dev.dao.gen.WmRoomPrivilegeMapper;
import cn.com.woxitech.dev.dao.gen.WmRoomUseronlineMapper;
import cn.com.woxitech.dev.dao.gen.WmUserFollowMapper;
import cn.com.woxitech.dev.dao.gen.WmUserMapper;
import cn.com.woxitech.dev.model.WmChathistoryFetch;
import cn.com.woxitech.dev.model.gen.TPptCode;
import cn.com.woxitech.dev.model.gen.TPptCodeExample;
import cn.com.woxitech.dev.model.gen.WmCatalogRoomExample;
import cn.com.woxitech.dev.model.gen.WmCatalogRoomKey;
import cn.com.woxitech.dev.model.gen.WmRoom;
import cn.com.woxitech.dev.model.gen.WmRoomChair;
import cn.com.woxitech.dev.model.gen.WmRoomChairExample;
import cn.com.woxitech.dev.model.gen.WmRoomChathistory;
import cn.com.woxitech.dev.model.gen.WmRoomChathistoryExample;
import cn.com.woxitech.dev.model.gen.WmRoomExample;
import cn.com.woxitech.dev.model.gen.WmRoomLike;
import cn.com.woxitech.dev.model.gen.WmRoomLikeExample;
import cn.com.woxitech.dev.model.gen.WmRoomPrivilege;
import cn.com.woxitech.dev.model.gen.WmRoomPrivilegeExample;
import cn.com.woxitech.dev.model.gen.WmRoomUseronline;
import cn.com.woxitech.dev.model.gen.WmRoomUseronlineExample;
import cn.com.woxitech.dev.model.gen.WmUser;
import cn.com.woxitech.dev.model.gen.WmUserExample;
import cn.com.woxitech.dev.model.gen.WmUserFollowExample;

@Service
public class RoomService {
	@Autowired
	WmRoomMapper roomMapper;
	@Autowired
	WmRoomPrivilegeMapper roomPrivilegeMapper;
	@Autowired
	WmRoomLikeMapper roomLikeMapper;
	@Autowired
	WmCatalogRoomMapper catalogRoomMapper;
	@Autowired
	WmUserMapper userMapper;
	@Autowired
	TPptCodeMapper pptCodeMapper;
	@Autowired
	WmUserFollowMapper userFollowMapper;

	@Autowired
	UserService userService;

	@Autowired
	WmRoomChairMapper roomChairMapper;

	@Autowired
	WmRoomUseronlineMapper roomUseronlineMapper;

	@Autowired
	WmRoomChathistoryMapper roomChathistoryMapper;

	public int countRoomByOrguuid(String orgUuid) {
		WmRoomExample example = new WmRoomExample();
		example.createCriteria().andOrguuidEqualTo(orgUuid);
		return roomMapper.countByExample(example);
	}

	public void insertRoom(WmRoom room) {
		roomMapper.insertSelective(room);
	}

	public List<WmRoom> getRoomsByOrgUuid(String orgUuid) {
		WmRoomExample example = new WmRoomExample();
		example.createCriteria().andOrguuidEqualTo(orgUuid);
		return roomMapper.selectByExample(example);
	}

	public WmRoom getRoomsByUuid(String uuid) {
		WmRoomExample example = new WmRoomExample();
		example.createCriteria().andUuidEqualTo(uuid);
		List<WmRoom> list = roomMapper.selectByExample(example);
		if (list.isEmpty()) {
			return null;
		} else {
			return list.get(0);
		}
	}

	public void updateByPrimaryKey(WmRoom room) {
		roomMapper.updateByPrimaryKeySelective(room);
	}

	public void updateByPrimaryKeyHard(WmRoom room) {
		roomMapper.updateByPrimaryKey(room);
	}

	public List<WmRoom> getRoomsByUserUuid(String uuid) {
		// TODO Auto-generated method stub
		WmRoomExample example = new WmRoomExample();
		example.createCriteria().andCreatoruuidEqualTo(uuid);
		example.setOrderByClause("time desc");
		List<WmRoom> list = roomMapper.selectByExample(example);
		return list;
	}

	public List<WmRoom> getRoomsByUuidList(List<String> roomUuidStrs, String orderby) {
		// TODO Auto-generated method stub

		WmRoomExample example = new WmRoomExample();
		example.createCriteria().andUuidIn(roomUuidStrs);
		example.setOrderByClause(orderby + " desc");
		return roomMapper.selectByExample(example);
	}

	public void addRoomChair(String uuid, String userid) {
		// TODO Auto-generated method stub
		WmRoomChair c = new WmRoomChair();
		c.setChairid(userid);
		c.setRoomid(uuid);
		roomChairMapper.insertSelective(c);
	}

	public List<WmRoom> getPublicRoomsByUserUuidAndOrgUuid(String uuid, String orguuid, String orderby) {
		// TODO Auto-generated method stub

		WmRoomExample example = new WmRoomExample();
		example.createCriteria().andCreatoruuidEqualTo(uuid).andOrguuidEqualTo(orguuid).andRoomTypeNotEqualTo("0");
		example.setOrderByClause(orderby + " desc");
		return roomMapper.selectByExample(example);
	}

	public List<WmRoom> getPublicRoomsByUserUuidAndOrgUuidStrList(String uuid, List<String> orgUuidList,
			String orderby) {
		// TODO Auto-generated method stub
		WmRoomExample example = new WmRoomExample();
		example.createCriteria().andCreatoruuidEqualTo(uuid).andOrguuidIn(orgUuidList).andRoomTypeNotEqualTo("0");
		example.setOrderByClause(orderby + " desc");
		return roomMapper.selectByExample(example);
	}

	public List<WmRoom> getHeadRefreshRoomsByUuidList(List<String> roomUuidStrs, String orderby, int pos, int size,
			long timestamp, boolean show_private, String keyword) {
		// TODO Auto-generated method stub

		WmRoomExample e = new WmRoomExample();

		cn.com.woxitech.dev.model.gen.WmRoomExample.Criteria criteria = e.createCriteria();
		if (pos == 0) {
			criteria.andUuidIn(roomUuidStrs);
			if (!show_private) {
				criteria.andRoomTypeNotEqualTo("0");
			}
		} else {

			criteria.andUuidIn(roomUuidStrs).andCreatetimeGreaterThanOrEqualTo(new Date(timestamp));
			if (!show_private) {
				criteria.andRoomTypeNotEqualTo("0");
			}
		}
		if (keyword != null) {
			criteria.andNameLike("%" + keyword + "%");
		}
		e.setOrderByClause(orderby + " desc limit " + pos + "," + size);
		return roomMapper.selectByExample(e);
	}

	public List<WmRoom> getFooterRefreshRoomsByUuidList(List<String> roomUuidStrs, String orderby, int pos, int size,
			long timestamp, boolean show_private, String keyword) {

		// 计算出在大于timestamp时刻新创建的room的数量

		WmRoomExample e1 = new WmRoomExample();
		cn.com.woxitech.dev.model.gen.WmRoomExample.Criteria criteria1 = e1.createCriteria();

		criteria1.andUuidIn(roomUuidStrs).andCreatetimeGreaterThanOrEqualTo(new Date(timestamp));

		if (!show_private) {
			criteria1.andRoomTypeNotEqualTo("0");
		}
		if (keyword != null) {
			criteria1.andNameLike("%" + keyword + "%");
		}

		int count = roomMapper.countByExample(e1);

		WmRoomExample example1 = new WmRoomExample();
		cn.com.woxitech.dev.model.gen.WmRoomExample.Criteria criteria2 = example1.createCriteria();

		criteria2.andUuidIn(roomUuidStrs);
		if (!show_private) {
			criteria2.andRoomTypeNotEqualTo("0");
		}
		if (keyword != null) {
			criteria2.andNameLike("%" + keyword + "%");
		}

		example1.setOrderByClause(orderby + " desc limit " + (pos + count) + ", " + size);
		return roomMapper.selectByExample(example1);
	}

	public List<WmRoom> getHeadRefreshRoomsByUuidList(List<String> roomUuidStrs, String orderby, int pos, int size,
			long timestamp, boolean show_private) {
		return getHeadRefreshRoomsByUuidList(roomUuidStrs, orderby, pos, size, timestamp, show_private, null);
	}

	public List<WmRoom> getFooterRefreshRoomsByUuidList(List<String> roomUuidStrs, String orderby, int pos, int size,
			long timestamp, boolean show_private) {
		// TODO Auto-generated method stub
		return getFooterRefreshRoomsByUuidList(roomUuidStrs, orderby, pos, size, timestamp, show_private, null);
	}

	public List<String> getRoomUuidsByUuidAndOrgUuid(String uuid, String orguuid) {
		// TODO Auto-generated method stub

		List<String> roomUUidStr = new ArrayList<String>();

		WmRoomExample example = new WmRoomExample();
		example.createCriteria().andCreatoruuidEqualTo(uuid).andOrguuidEqualTo(orguuid);
		List<WmRoom> myRooms = roomMapper.selectByExample(example);

		for (WmRoom cr : myRooms) {
			roomUUidStr.add(cr.getUuid());
		}

		return roomUUidStr;
	}

	public void deleteByPrimarykey(WmRoom room) {
		// TODO Auto-generated method stub
		this.roomMapper.deleteByPrimaryKey(room.getId());
	}

	public void saveUserOnline(String useruuid, String roomuuid) {
		// TODO Auto-generated method stub
		WmRoomUseronline record = new WmRoomUseronline();
		record.setRoomuuid(roomuuid);
		record.setUseruuid(useruuid);
		record.setUpdatetime(System.currentTimeMillis());
		roomUseronlineMapper.insertSelective(record);
	}

	public void removeUserOnline(String useruuid, String roomuuid) {
		// TODO Auto-generated method stub
		WmRoomUseronlineExample e = new WmRoomUseronlineExample();
		e.createCriteria().andRoomuuidEqualTo(roomuuid).andUseruuidEqualTo(useruuid);
		roomUseronlineMapper.deleteByExample(e);
	}

	public void saveChathistory(WmRoomChathistory ch) {
		// TODO Auto-generated method stub
		roomChathistoryMapper.insertSelective(ch);
	}

	public void saveMaxConcurrentUser(String roomuuid, int current) {
		// TODO Auto-generated method stub
		WmRoomExample e = new WmRoomExample();
		e.createCriteria().andUuidEqualTo(roomuuid);
		List<WmRoom> list = this.roomMapper.selectByExample(e);

		if (list != null && !list.isEmpty()) {
			WmRoom room = list.get(0);
			int max = room.getMaxconcurrent().intValue();

			if (max < current) {
				max = current;
				room.setMaxconcurrent(new Integer(max));
				roomMapper.updateByPrimaryKeySelective(room);
			}
		}

	}

	public int countOnlineUser(String roomuuid, boolean includeVirtual) {
		// TODO Auto-generated method stub
		WmRoomUseronlineExample e = new WmRoomUseronlineExample();
		if (includeVirtual) {
			e.createCriteria().andRoomuuidEqualTo(roomuuid);
		} else {
			e.createCriteria().andRoomuuidEqualTo(roomuuid).andUseruuidIsNotNull();
		}
		e.createCriteria().andRoomuuidEqualTo(roomuuid);
		int count = this.roomUseronlineMapper.countByExample(e);
		return count;
	}

	public List<WmUser> getOnlineUser(String roomuuid, int count) {
		// TODO Auto-generated method stub

		List<WmUser> users = new ArrayList<WmUser>();
		WmRoomUseronlineExample e = new WmRoomUseronlineExample();
		e.createCriteria().andRoomuuidEqualTo(roomuuid);
		e.setOrderByClause("updatetime desc limit 0, " + count);
		List<WmRoomUseronline> onlines = this.roomUseronlineMapper.selectByExample(e);

		for (WmRoomUseronline online : onlines) {
			users.add(userService.getUserByUuid(online.getUseruuid()));
		}

		return users;
	}

	public List<WmChathistoryFetch> fetchHistory(String room_uuid, String timestamp, int size) {
		// TODO Auto-generated method stub
		List<WmChathistoryFetch> list = new ArrayList<WmChathistoryFetch>();

		WmRoomChathistoryExample e1 = new WmRoomChathistoryExample();
		long _timestamp = Long.parseLong(timestamp);
		e1.createCriteria().andTimeLessThan(_timestamp).andRoomuuidEqualTo(room_uuid).andTypeNotEqualTo("login")
				.andTypeNotEqualTo("logout").andStateNotEqualTo("0");

		e1.setOrderByClause("time desc limit 0, " + size);
		List<WmRoomChathistory> list_c = this.roomChathistoryMapper.selectByExample(e1);

		if (list_c != null && !list_c.isEmpty()) {

			List<String> listUserStr = new ArrayList<String>();

			Map<String, String> map_temp1 = new HashMap<String, String>();

			for (int i = 0; i < list_c.size(); i++) {

				map_temp1.put(list_c.get(i).getUseruuid(), list_c.get(i).getUseruuid());
			}

			Iterator it = map_temp1.keySet().iterator();
			while (it.hasNext()) {
				String key = it.next().toString();
				listUserStr.add(key);
			}

			WmUserExample e2 = new WmUserExample();
			e2.createCriteria().andUuidIn(listUserStr);
			List<WmUser> list_u = userMapper.selectByExample(e2);

			Map<String, WmUser> map_User = new HashMap<String, WmUser>();

			for (int i = 0; i < list_u.size(); i++) {
				list_u.get(i).setuAccessToken("");
				list_u.get(i).setuExpiresIn(0);
				list_u.get(i).setuRefreshToken("");
				list_u.get(i).setTel("");
				list_u.get(i).setPwd("你猜");
				list_u.get(i).setCreatetime(new Date());
				list_u.get(i).setDyAccessToken("");
				list_u.get(i).setDyExpiresIn(0);
				list_u.get(i).setDyOpenid("");
				list_u.get(i).setDyRefreshToken("");
				list_u.get(i).setDyScope("");
				list_u.get(i).setEmail("");
				list_u.get(i).setLastlogin(new Date());
				list_u.get(i).setOpenAccessToken("");
				list_u.get(i).setOpenExpiresIn(0);
				list_u.get(i).setOpenRefreshToken("");
				list_u.get(i).setOpenRefreshToken("");
				list_u.get(i).setOpenScope("");
				list_u.get(i).setWxOpenid("");
				list_u.get(i).setWxCountry("");
				list_u.get(i).setWxCity("");
				map_User.put(list_u.get(i).getUuid(), list_u.get(i));
			}

			for (int i = 0; i < list_c.size(); i++) {
				WmChathistoryFetch cf = new WmChathistoryFetch();
				cf.setWmRoomChathistory(list_c.get(i));
				cf.setWmUser(map_User.get(list_c.get(i).getUseruuid()));
				list.add(cf);
			}

		}

		return list;
	}

	public int addAttendCount(WmRoom r) {
		// TODO Auto-generated method stub

		int now = r.getAttendeecount();
		r.setAttendeecount(now + 1);
		this.updateByPrimaryKey(r);
		return (now + 1);
	}

	public int doesUserLikeroom(String useruuid, String roomuuid) {
		WmRoomLikeExample e = new WmRoomLikeExample();
		e.createCriteria().andUseruuidEqualTo(useruuid).andRoomuuidEqualTo(roomuuid);
		int c = roomLikeMapper.countByExample(e);
		return (c > 0) ? 1 : 0;
	}

	public int getRoomLikeCount(String roomuuid) {
		// TODO Auto-generated method stub
		WmRoomLikeExample e = new WmRoomLikeExample();
		e.createCriteria().andRoomuuidEqualTo(roomuuid);
		int c = roomLikeMapper.countByExample(e);
		return c;
	}

	public int getRoomLikeCount(String roomuuid, String useruuid) {
		// TODO Auto-generated method stub

		WmRoomLikeExample e = new WmRoomLikeExample();
		e.createCriteria().andUseruuidEqualTo(useruuid).andRoomuuidEqualTo(roomuuid);
		int c = roomLikeMapper.countByExample(e);
		return c;
	}

	public void saveRoomLike(String roomuuid, String useruuid) {
		// TODO Auto-generated method stub

		if (this.getRoomLikeCount(roomuuid, useruuid) == 0) {

			WmRoomLike rl = new WmRoomLike();
			rl.setLiketime(new Date(System.currentTimeMillis()));
			rl.setRoomuuid(roomuuid);
			rl.setUseruuid(useruuid);

			this.roomLikeMapper.insertSelective(rl);
		}
	}

	public List<String> getMyLikedRoomUuidsByUserUuid(String useruuid) {
		// TODO Auto-generated method stub
		WmRoomLikeExample e = new WmRoomLikeExample();
		e.createCriteria().andUseruuidEqualTo(useruuid);
		List<WmRoomLike> l = roomLikeMapper.selectByExample(e);
		List<String> l_str = new ArrayList<String>();
		for (WmRoomLike item : l) {
			l_str.add(item.getRoomuuid());
		}

		return l_str;
	}

	public void deleteRoomLike(String roomuuid, String useruuid) {
		// TODO Auto-generated method stub
		if (this.getRoomLikeCount(roomuuid, useruuid) != 0) {
			WmRoomLikeExample e = new WmRoomLikeExample();
			e.createCriteria().andRoomuuidEqualTo(roomuuid).andUseruuidEqualTo(useruuid);
			this.roomLikeMapper.deleteByExample(e);
		}
	}

	public List<String> getRoomUuidsByFollowingUsersUuid(String useruuid) {
		// TODO Auto-generated method stub

		List<String> followingUserStrlist = userService.getFollowingUsersStrsByUuid(useruuid);

		if (followingUserStrlist.isEmpty()) {
			return new ArrayList<String>();
		}

		List<WmRoom> rooms = this.getRoomsByUserUuidStrList(followingUserStrlist);

		if (rooms.isEmpty()) {
			return new ArrayList<String>();
		}

		List<String> list = new ArrayList<String>();
		for (WmRoom r : rooms) {
			list.add(r.getUuid());
		}

		return list;
	}

	public List<String> getRoomUuidsByFollowingUsersUuid(String useruuid, String orguuid) {
		// TODO Auto-generated method stub

		List<String> followingUserStrlist = userService.getFollowingUsersStrsByUuid(useruuid);

		if (followingUserStrlist.isEmpty()) {
			return new ArrayList<String>();
		}

		List<WmRoom> rooms = this.getRoomsByUserUuidStrListAndOrgUuid(followingUserStrlist, orguuid);

		if (rooms.isEmpty()) {
			return new ArrayList<String>();
		}

		List<String> list = new ArrayList<String>();
		for (WmRoom r : rooms) {
			list.add(r.getUuid());
		}

		return list;
	}

	public List<WmRoom> getRoomsByUserUuidStrList(List<String> followingUserStrlist) {
		// TODO Auto-generated method stub
		WmRoomExample e = new WmRoomExample();
		e.createCriteria().andCreatoruuidIn(followingUserStrlist);
		return roomMapper.selectByExample(e);
	}

	public List<WmRoom> getRoomsByUserUuidStrListAndOrgUuid(List<String> followingUserStrlist, String orguuid) {
		// TODO Auto-generated method stub
		WmRoomExample e = new WmRoomExample();
		e.createCriteria().andCreatoruuidIn(followingUserStrlist).andOrguuidEqualTo(orguuid);
		return roomMapper.selectByExample(e);
	}

	public List<WmRoom> getRoomsByUserUuidStrListAndOrgUuidStrList(List<String> users_l, List<String> orgs_l) {
		// TODO Auto-generated method stub
		WmRoomExample e = new WmRoomExample();
		e.createCriteria().andCreatoruuidIn(users_l).andOrguuidIn(orgs_l);
		e.setOrderByClause("time desc");
		return roomMapper.selectByExample(e);
	}

	public List<String> getRoomUuidsByCatalogUid(String cataloguuid) {
		// TODO Auto-generated method stub

		WmCatalogRoomExample e = new WmCatalogRoomExample();
		e.createCriteria().andCataloguuidEqualTo(cataloguuid);
		List<WmCatalogRoomKey> l = this.catalogRoomMapper.selectByExample(e);

		List<String> ls = new ArrayList<String>();

		for (int i = 0; i < l.size(); i++) {
			ls.add(l.get(i).getRoomuuid());
		}

		return ls;

	}

	// public int countRoomsOfSomeBodyBySearchTitle(String w, int _p, int _s,
	// String useruuid, String c) {
	// // TODO Auto-generated method stub
	// WmRoomExample e = new WmRoomExample();
	// if(!c.equals("")){
	// List<String> list = this.getRoomUuidsByCatalogUid(c);
	// if(list !=null){
	// e.createCriteria().andNameLike("%"+w+"%").andCreatoruuidEqualTo(useruuid).andUuidIn(list);
	// }else{
	// return 0;
	// }
	// }else{
	// e.createCriteria().andNameLike("%"+w+"%").andCreatoruuidEqualTo(useruuid);
	// }
	//
	// e.setOrderByClause("time desc limit "+((_p-1)*_s)+","+_s);
	// return this.roomMapper.countByExample(e);
	// }
	//
	// public List<WmRoom> getRoomsOfSomeBodyBySearchTitle(String w, int _p, int
	// _s, String useruuid, String c) {
	// // TODO Auto-generated method stub
	// WmRoomExample e = new WmRoomExample();
	// if(!c.equals("")){
	// List<String> list = this.getRoomUuidsByCatalogUid(c);
	// if(list !=null){
	// e.createCriteria().andNameLike("%"+w+"%").andCreatoruuidEqualTo(useruuid).andUuidIn(list);
	// }else{
	// return new ArrayList<WmRoom>();
	// }
	// }else{
	// e.createCriteria().andNameLike("%"+w+"%").andCreatoruuidEqualTo(useruuid);
	// }
	//
	// e.setOrderByClause("time desc limit "+((_p-1)*_s)+","+_s);
	// return
	// }

	public Map<String, Object> getRoomsOfSomeBodyBySearchTitle(String w, int _p, int _s, String useruuid, String c) {
		// TODO Auto-generated method stub

		Map<String, Object> mp = new HashMap<String, Object>();

		mp.put("total", 0);
		mp.put("rooms", new ArrayList<WmRoom>());

		WmRoomExample e = new WmRoomExample();
		if (!c.equals("")) {
			List<String> list = this.getRoomUuidsByCatalogUid(c);
			if (list != null) {
				e.createCriteria().andNameLike("%" + w + "%").andCreatoruuidEqualTo(useruuid).andUuidIn(list);
			} else {
				return mp;
			}
		} else {
			e.createCriteria().andNameLike("%" + w + "%").andCreatoruuidEqualTo(useruuid);

		}

		List<WmRoomPrivilege> l_p = this.getRoomsPrivilegeByUserUuid(useruuid);
		if (l_p != null && !l_p.isEmpty()) {
			List<String> l_p_str = new ArrayList<String>();
			for (int i = 0; i < l_p.size(); i++) {
				l_p_str.add(l_p.get(i).getRoomid());
			}

			e.or().andNameLike("%" + w + "%").andUuidIn(l_p_str);

		}

		e.setOrderByClause("time desc limit " + ((_p - 1) * _s) + "," + _s);
		e.setDistinct(true);

		mp.put("total", this.roomMapper.countByExample(e));
		mp.put("rooms", this.roomMapper.selectByExample(e));
		return mp;
	}

	private List<WmRoomPrivilege> getRoomsPrivilegeByUserUuid(String useruuid) {
		// TODO Auto-generated method stub
		WmRoomPrivilegeExample e = new WmRoomPrivilegeExample();
		e.createCriteria().andUseridEqualTo(useruuid);
		return this.roomPrivilegeMapper.selectByExample(e);
	}

	public boolean hasManagePrivilegeForRoom(WmRoom room, String useruuid) {
		// TODO Auto-generated method stub

		if (room.getCreatoruuid().equals(useruuid)) {
			return true;
		} else {
			WmRoomPrivilegeExample e = new WmRoomPrivilegeExample();
			e.createCriteria().andUseridEqualTo(useruuid).andRoomidEqualTo(room.getUuid());
			int c = roomPrivilegeMapper.countByExample(e);
			if (c != 0) {
				return true;
			} else {
				return false;
			}
		}

	}

	public List<Map<String, Object>> getRoomChairs(String roomuuid) {
		// TODO Auto-generated method stub

		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();

		WmRoomChairExample e1 = new WmRoomChairExample();
		e1.createCriteria().andRoomidEqualTo(roomuuid);
		List<WmRoomChair> l1 = this.roomChairMapper.selectByExample(e1);

		if (l1 == null || l1.isEmpty()) {
			return list;
		}

		List<String> chair_user_uuid_str_list = new ArrayList<String>();
		for (int i = 0; i < l1.size(); i++) {
			chair_user_uuid_str_list.add(l1.get(i).getChairid());
		}

		List<WmUser> users = new ArrayList<WmUser>();
		WmUserExample e2 = new WmUserExample();
		e2.createCriteria().andUuidIn(chair_user_uuid_str_list);
		users = this.userMapper.selectByExample(e2);
		for (int j = 0; j < users.size(); j++) {

			Map<String, Object> m = new HashMap<String, Object>();
			m.put("uuid", users.get(j).getUuid());
			m.put("name", users.get(j).getName());
			m.put("email", users.get(j).getEmail());
			m.put("tel", users.get(j).getTel());
			m.put("headimgurl", users.get(j).getWxHeadimgurl());
			list.add(m);
		}

		return list;
	}

	public int countRoomChairs(String roomuuid, String userid) {
		// TODO Auto-generated method stub
		WmRoomChairExample e = new WmRoomChairExample();
		e.createCriteria().andRoomidEqualTo(roomuuid).andChairidEqualTo(userid);
		return this.roomChairMapper.countByExample(e);
	}

	public void removeRoomChair(String roomuuid, String useruuid) {
		// TODO Auto-generated method stub
		WmRoomChairExample e = new WmRoomChairExample();
		e.createCriteria().andRoomidEqualTo(roomuuid).andChairidEqualTo(useruuid);
		this.roomChairMapper.deleteByExample(e);
	}

	public List<WmRoom> getHotRooms(int size) {
		// TODO Auto-generated method stub
		WmRoomExample e = new WmRoomExample();
		e.createCriteria().andRoomTypeNotEqualTo("0");
		String orderby = "attendeeCount";
		e.setOrderByClause(orderby + " desc limit 0, " + size);
		return this.roomMapper.selectByExample(e);
	}

	public List<WmRoom> getNewRooms(int size) {
		// TODO Auto-generated method stub
		WmRoomExample e = new WmRoomExample();
		e.createCriteria().andRoomTypeNotEqualTo("0");
		String orderby = "createtime";
		e.setOrderByClause(orderby + " desc limit 0, " + size);
		return this.roomMapper.selectByExample(e);
	}

	public void removeHistoryItem(String msgid) {
		// TODO Auto-generated method stub
		WmRoomChathistory r = new WmRoomChathistory();
		r.setMsgid(msgid);
		r.setState("0");
		WmRoomChathistoryExample e = new WmRoomChathistoryExample();
		e.createCriteria().andMsgidEqualTo(msgid);
		this.roomChathistoryMapper.updateByExampleSelective(r, e);
	}

	public List<String> getRoomsUuidsByUserUuid(String useruuid) {
		// TODO Auto-generated method stub

		List<String> roomUUidStr = new ArrayList<String>();

		WmRoomExample example = new WmRoomExample();
		example.createCriteria().andCreatoruuidEqualTo(useruuid);
		List<WmRoom> myRooms = roomMapper.selectByExample(example);

		for (WmRoom cr : myRooms) {
			roomUUidStr.add(cr.getUuid());
		}

		return roomUUidStr;
	}

	public void savePPTAuthCode(String code, String userid) {
		// TODO Auto-generated method stub
		TPptCodeExample e = new TPptCodeExample();
		e.createCriteria().andUseruuidEqualTo(userid);
		List<TPptCode> list = pptCodeMapper.selectByExample(e);
		if(list.isEmpty()){
			
			TPptCode record = new TPptCode();
			record.setCode(code);
			record.setTime(new Date(System.currentTimeMillis()));
			record.setUseruuid(userid);
			pptCodeMapper.insertSelective(record);
		}else{
			
			TPptCode r = list.get(0);
			r.setCode(code);
			r.setTime(new Date(System.currentTimeMillis()));
			pptCodeMapper.updateByPrimaryKeySelective(r);
		}
		

	}

	public String verifyPPTAuthCode(String code) {
		// TODO Auto-generated method stub
		
		Date now60s = new Date(System.currentTimeMillis() - 60000); 
		TPptCodeExample e = new TPptCodeExample();
		e.createCriteria().andCodeEqualTo(code).andTimeGreaterThanOrEqualTo(now60s);
		List<TPptCode> list = pptCodeMapper.selectByExample(e);
		if(list.isEmpty()){
			return null;
		}else{
			return list.get(0).getUseruuid();
		}
	}
}
